package com.photography.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.photography.annotation.RequireRole;
import com.photography.common.Result;
import com.photography.entity.ShippingAddress;
import com.photography.service.ShippingAddressService;

import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/address")
@RequireRole("user")
public class ShippingAddressController {

    @Resource
    private ShippingAddressService shippingAddressService;

    /**
     * 新增收货地址
     *
     * @param address 地址信息
     * @return 结果
     */
    @PostMapping("/add")
    public Result addAddress(@RequestBody ShippingAddress address) {
        return shippingAddressService.addAddress(address);
    }

    /**
     * 删除收货地址
     *
     * @param id 地址id
     * @return 结果
     */
    @DeleteMapping("/{id}")
    public Result deleteAddress(@PathVariable Integer id) {
        return shippingAddressService.deleteAddress(id);
    }

    /**
     * 修改收货地址
     *
     * @param address 修改地址信息
     * @return 结果
     */
    @PutMapping("/update")
    public Result updateAddress(@RequestBody ShippingAddress address) {
        return shippingAddressService.updateAddress(address);
    }

    /**
     * 设置默认收货地址
     *
     * @param id 地址id
     * @return 结果
     */
    @PutMapping("/default/{id}")
    public Result setDefaultAddress(@PathVariable Integer id) {
        return shippingAddressService.setDefaultAddress(id);
    }

    /**
     * 根据用户ID分页查询收货地址
     *
     * @return 结果
     */
    @GetMapping("/page")
    public Result getAddressPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<ShippingAddress> page = new Page<>(current, size);
        return Result.success(shippingAddressService.getAddressPage(page), "查询成功");
    }
}
